﻿using System;
using System.Linq;
using EFDataModel.Enums;

namespace EFDataModel.DataCreation
{
    public class FillLists
    {
        #region Member
        public EFModelContainer AzModelContainer { get; set; }
        #endregion

        #region Konstruktor
        public FillLists(EFModelContainer azModelContainer)
        {
            AzModelContainer = azModelContainer;
            //DB Erstellen, wenn diese noch nicht existiert.
            azModelContainer.Database.CreateIfNotExists();
        }
        #endregion

        #region Public Functions
        /// <summary>
        /// Erstellen der ListItems, wenn diese noch keine Werte enthalten.
        /// </summary>
        public void CreateListItems()
        {
            //Erstellen der UserCategories und UserGroups
            CreateUserCategories();
        }
        #endregion

        #region Private functions
        /// <summary>
        /// Ein paar standardkategorien für User anlegen
        /// </summary>
        private void CreateUserCategories()
        {
            if (AzModelContainer.EFCategoryMenge.Count() == 0)
            {
                //Systemkategorie anlegen für den Mitarbeiter
                UserCategory userCategory = new UserCategory() { Name = "Mitarbeiter", TypeId = 1, EFCategoryType = EFCategoryTypes.UserCategory, IsSystem = true };
                AzModelContainer.EFCategoryMenge.Add(userCategory);

                userCategory = new UserCategory() { Name = "Azubi", TypeId = 2, EFCategoryType = EFCategoryTypes.UserCategory };
                AzModelContainer.EFCategoryMenge.Add(userCategory);

                userCategory = new UserCategory() { Name = "Extern", TypeId = 3, IsSystem = true, EFCategoryType = EFCategoryTypes.UserCategory };
                AzModelContainer.EFCategoryMenge.Add(userCategory);

                //Ein paar Standardusergruppen erstellen
                UserGroup efGroup = new UserGroup() { Name = "Mitarbeiter Modul", TypeId = 1, EFCategoryType = EFCategoryTypes.UserGroups };
                AzModelContainer.EFCategoryMenge.Add(efGroup);

                efGroup = new UserGroup() { Name = "Bereichlseiter", TypeId = 2, EFCategoryType = EFCategoryTypes.UserGroups };
                AzModelContainer.EFCategoryMenge.Add(efGroup);

                efGroup = new UserGroup() { Name = "Gruppenleiter", TypeId = 3, IsSystem = true, EFCategoryType = EFCategoryTypes.UserGroups };
                AzModelContainer.EFCategoryMenge.Add(efGroup);

                //Anlegen der UserTitles
                UserTitle userTitle = new UserTitle() {Name = "Mann", TypeId = (int) EUserTitles.Man, IsSystem = true, EFCategoryType = EFCategoryTypes.UserTitles};
                AzModelContainer.EFCategoryMenge.Add(userTitle);

                userTitle = new UserTitle() { Name = "Frau", TypeId = (int)EUserTitles.Women, IsSystem = true, EFCategoryType = EFCategoryTypes.UserTitles };
                AzModelContainer.EFCategoryMenge.Add(userTitle);

                userTitle = new UserTitle() { Name = "Familie", TypeId = (int)EUserTitles.Family, IsSystem = true, EFCategoryType = EFCategoryTypes.UserTitles };
                AzModelContainer.EFCategoryMenge.Add(userTitle);

                //Erstellen der Userrechte
                CreateUserRights();

                AzModelContainer.SaveChanges();
            }
        }

        /// <summary>
        /// Anlegen aller im System zur verfügung stehenden Rechte.
        /// </summary>
        private void CreateUserRights()
        {
            //Ein Userrecht ist immer ein Systemrecht!
            UserRight userRight = new UserRight() { Name = EUserRights.SetUserGroups.ToString(), EFModule = EFModules.Member, TypeId = (int)EUserRights.SetUserGroups, EFCategoryType = EFCategoryTypes.UserRights, IsSystem = true };
            AzModelContainer.EFCategoryMenge.Add(userRight);

            userRight = new UserRight() { Name = EUserRights.UserDetails.ToString(), EFModule = EFModules.Member, TypeId = (int)EUserRights.UserDetails, EFCategoryType = EFCategoryTypes.UserRights, IsSystem = true };
            AzModelContainer.EFCategoryMenge.Add(userRight);

            userRight = new UserRight() { Name = EUserRights.UserCreate.ToString(), EFModule = EFModules.Member, TypeId = (int)EUserRights.UserCreate, EFCategoryType = EFCategoryTypes.UserRights, IsSystem = true };
            AzModelContainer.EFCategoryMenge.Add(userRight);

            userRight = new UserRight() { Name = EUserRights.UserModule.ToString(), EFModule = EFModules.Member, TypeId = (int)EUserRights.UserModule, EFCategoryType = EFCategoryTypes.UserRights, IsSystem = true };
            AzModelContainer.EFCategoryMenge.Add(userRight);

            userRight = new UserRight() { Name = EUserRights.UserEdit.ToString(), EFModule = EFModules.Member, TypeId = (int)EUserRights.UserEdit, EFCategoryType = EFCategoryTypes.UserRights, IsSystem = true };
            AzModelContainer.EFCategoryMenge.Add(userRight);

            userRight = new UserRight() { Name = EUserRights.SetUserRights.ToString(), EFModule = EFModules.Member, TypeId = (int)EUserRights.SetUserRights, EFCategoryType = EFCategoryTypes.UserRights, IsSystem = true };
            AzModelContainer.EFCategoryMenge.Add(userRight);

            userRight = new UserRight() { Name = EUserRights.CreateDynamicDataTypes.ToString(), EFModule = EFModules.DynamicData, TypeId = (int)EUserRights.CreateDynamicDataTypes, EFCategoryType = EFCategoryTypes.UserRights, IsSystem = true };
            AzModelContainer.EFCategoryMenge.Add(userRight);

            userRight = new UserRight() { Name = EUserRights.SetUserPassword.ToString(), EFModule = EFModules.Member, TypeId = (int)EUserRights.SetUserPassword, EFCategoryType = EFCategoryTypes.UserRights, IsSystem = true };
            AzModelContainer.EFCategoryMenge.Add(userRight);

            userRight = new UserRight() { Name = EUserRights.WorktimeModule.ToString(), EFModule = EFModules.Worktime, TypeId = (int)EUserRights.WorktimeModule, EFCategoryType = EFCategoryTypes.UserRights, IsSystem = true };
            AzModelContainer.EFCategoryMenge.Add(userRight);
            
            userRight = new UserRight() { Name = EUserRights.WorktimeModule.ToString(), EFModule = EFModules.Worktime, TypeId = (int)EUserRights.WorktimeDeleteEntry, EFCategoryType = EFCategoryTypes.UserRights, IsSystem = true };
            AzModelContainer.EFCategoryMenge.Add(userRight);

            userRight = new UserRight() { Name = EUserRights.WorktimeEdit.ToString(), EFModule = EFModules.Worktime, TypeId = (int)EUserRights.WorktimeEdit, EFCategoryType = EFCategoryTypes.UserRights, IsSystem = true };
            AzModelContainer.EFCategoryMenge.Add(userRight);

            userRight = new UserRight() { Name = EUserRights.WorktimeSaveHoliday.ToString(), EFModule = EFModules.Worktime, TypeId = (int)EUserRights.WorktimeSaveHoliday, EFCategoryType = EFCategoryTypes.UserRights, IsSystem = true };
            AzModelContainer.EFCategoryMenge.Add(userRight);

            userRight = new UserRight() { Name = EUserRights.WorktimeSettingsEdit.ToString(), EFModule = EFModules.Worktime, TypeId = (int)EUserRights.WorktimeSettingsEdit, EFCategoryType = EFCategoryTypes.UserRights, IsSystem = true };
            AzModelContainer.EFCategoryMenge.Add(userRight);
        }
        #endregion
    }
}
